<?php

namespace App\Http\Controllers\News;

use App\Model\Classify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ClassifyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $classify=Classify::get();
        return view('news.classify.index',compact('classify'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        $data= Classify::get();
        return view('news.classify.create',compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
//        dd($request->toArray());
       $data=$request->all();
       Classify::create($data);
//        return redirect()->route('news.classify.index')->with('success','添加分类成功');
        return redirect()->route('news.lists');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Model\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function show(Classify $classify)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function edit(Classify $classify)
    {
       $data= $classify->getClass($classify);
        return view('news.classify.edit',compact('data','classify'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Classify $classify)
    {
        $classify->update($request->all());
        return redirect()->route('news.classify.index')->with('success','编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Model\Classify  $classify
     * @return \Illuminate\Http\Response
     */
    public function destroy(Classify $classify)
    {
        $data=$classify->getChild($classify);

       if($data){
         return back()->with('warning','该分类下含有其他分类，请先删除子分类');
       }else{
           $classify->delete();
           return back()->with('success','删除成功');
       }
    }
}
